package avicit.bdp.dgs.mdm.utils.api.request;

import avicit.bdp.dgs.mdm.utils.api.header.HeaderAdapter;
import avicit.bdp.dgs.mdm.utils.api.header.HeaderAdapterUtil;
import avicit.bdp.dgs.mdm.utils.enums.Method;
import avicit.platform6.core.exception.BusinessException;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PostRequestAdapter extends RequestAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(PostRequestAdapter.class);

    private RestTemplate restTemplate;

    public PostRequestAdapter(int timeout) {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(timeout);
        requestFactory.setReadTimeout(timeout);
        restTemplate = new RestTemplate(requestFactory);

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        messageConverters.add(new FormHttpMessageConverter());

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
        messageConverters.add(converter);

        restTemplate.getMessageConverters().addAll(messageConverters);
    }

    public static PostRequestAdapter instance(int timeout) {
        return new PostRequestAdapter(timeout);
    }

    /**
     * 调用API接口
     * @param url
     * @return
     */
    public String callApi(String applicationCode, String authCode, String logId, String data, String url){
        try{
            HeaderAdapter headerAdapter = HeaderAdapterUtil.getHeaderAdapter(Method.POST.getName());
            HttpEntity<Object> httpEntity = (HttpEntity<Object>) headerAdapter.buildHttpEntity(applicationCode, authCode, logId, data);
            ResponseEntity<String> result = restTemplate.postForEntity(url, httpEntity , String.class);

            parseResponseData(result);

            LOG.info("POST方式调用接口成功");
            return "";
        }catch (Exception e){
            e.printStackTrace();
            String msg = String.format("调用API接口失败,%s", e.getMessage());
            LOG.error(msg);
            throw new BusinessException(msg);
        }
    }

}
